package com.defence.costomapp.activity.statistics;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.defence.costomapp.R;
import com.defence.costomapp.base.BaseNewActivity;
import com.defence.costomapp.db.LineHistoryModel;
import com.defence.costomapp.myinterface.RVItemClickListener;
import com.defence.costomapp.net.Constant;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


@Route(path = "/statistics/AnalysisFilterXYActivity")
public class AnalysisFilterXYActivity extends BaseNewActivity implements View.OnClickListener {

    @BindView(R.id.back)
    TextView back;
    @BindView(R.id.middle_title)
    TextView middleTitle;
    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.rb_day)
    RadioButton rbDay;
    @BindView(R.id.rb_mouth)
    RadioButton rbMouth;
    @BindView(R.id.rg_date)
    RadioGroup rgDate;
    @BindView(R.id.rb_left)
    RadioButton rbLeft;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.rb_right)
    RadioButton rbRight;
    @BindView(R.id.tv_description2)
    TextView tvDescription2;
    @BindView(R.id.tv_filterCondition)
    TextView tvFilterCondition;
    @BindView(R.id.tv_communityGoods)
    TextView tvCommunityGoods;
    @BindView(R.id.tv_filterCondition_right)
    TextView tvFilterConditionRight;
    @BindView(R.id.tv_communityGoods_right)
    TextView tvCommunityGoodsRight;
    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.tv_historyRecord)
    TextView tvHistoryRecord;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.lv_historyRecord)
    ListView lvHistoryRecord;

    //日月视图
    private String sdate = "1";
    private String mFiltername;
    private String mMachinename;
    private String mData_filternameright;
    private String mMachinenameright;
    private String mGoodsname;
    private String mGoodsnameright;
    private String mFilternames;
    private String mMachinenames;
    private String mData_filternamerights;
    private String mMachinenamerights;
    private String mGoodsnames;
    private String mGoodsnamerights;
    private String mRbleftright;
    private LineHistoryAdapter mLineHistoryAdapter;


    public static void start() {
        ARouter.getInstance().build("/statistics/AnalysisFilterXYActivity").navigation();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
//                SPUtils.getInstance(Constant.SHARED_NAME).clear();
                startActivity(new Intent(AnalysisFilterXYActivity.this, StatisticsActivity.class));
                finish();
                break;
            case R.id.right_title:
                startActivity(new Intent(AnalysisFilterXYActivity.this, MpLineChartActivity.class));
                break;
            case R.id.rb_left:
                rbLeft.setChecked(true);
                rbRight.setChecked(false);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "left");
                break;
            case R.id.rb_right:
                rbLeft.setChecked(false);
                rbRight.setChecked(true);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "right");
                break;
            case R.id.tv_filterCondition:
                rbLeft.setChecked(true);
                rbRight.setChecked(false);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "left");
                Intent intenttv = new Intent(AnalysisFilterXYActivity.this, AnalysisFilterActivity.class);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.VERTICALAXIS, "left");
                startActivity(intenttv);
                break;
            case R.id.tv_communityGoods:
                if ("点此定制纵向坐标系".equals(tvFilterCondition.getText().toString())) {
                    Toast.makeText(this, "请先选择纵向坐标条件", Toast.LENGTH_SHORT).show();
                } else {
                    rbLeft.setChecked(true);
                    rbRight.setChecked(false);
                    SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "left");
                    Intent intenttvs = new Intent(AnalysisFilterXYActivity.this, AnalysisFilter2Activity.class);
                    SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.VERTICALAXIS, "left");
                    startActivity(intenttvs);
                }
                break;
            case R.id.tv_filterCondition_right:
                rbLeft.setChecked(false);
                rbRight.setChecked(true);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "right");
                Intent intentrighttv = new Intent(AnalysisFilterXYActivity.this, AnalysisFilterActivity.class);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.VERTICALAXIS, "right");
                startActivity(intentrighttv);
                break;
            case R.id.tv_communityGoods_right:
                if ("点此定制纵向坐标系".equals(tvFilterConditionRight.getText().toString())) {
                    Toast.makeText(this, "请先选择纵向坐标条件", Toast.LENGTH_SHORT).show();
                } else {
                    rbLeft.setChecked(false);
                    rbRight.setChecked(true);
                    SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "right");
                    Intent intentrighttvs = new Intent(AnalysisFilterXYActivity.this, AnalysisFilter2Activity.class);
                    SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.VERTICALAXIS, "right");
                    startActivity(intentrighttvs);
                }
                break;
            case R.id.tv_description:
                rbLeft.setChecked(true);
                rbRight.setChecked(false);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "left");
                Intent intent = new Intent(AnalysisFilterXYActivity.this, AnalysisFilter2Activity.class);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.VERTICALAXIS, "lefts");
                startActivity(intent);
                break;
            case R.id.tv_description2:
                rbLeft.setChecked(false);
                rbRight.setChecked(true);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.RBLEFTRIGHT, "right");
                Intent intentright = new Intent(AnalysisFilterXYActivity.this, AnalysisFilter2Activity.class);
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.VERTICALAXIS, "rights");
                startActivity(intentright);
                break;
            case R.id.tv_historyRecord:
                Intent intenthisre = new Intent(AnalysisFilterXYActivity.this, LineHistoryActivity.class);
                startActivity(intenthisre);
//                Toast.makeText(this, "功能正在开发中", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_more:
                Intent intenthismore = new Intent(AnalysisFilterXYActivity.this, LineHistoryActivity.class);
                startActivity(intenthismore);
                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            SPUtils.getInstance(Constant.SHARED_NAME).clear();
            startActivity(new Intent(AnalysisFilterXYActivity.this, StatisticsActivity.class));
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_analysis_filter_xy;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {

        back.setText("返回");
        rightTitle.setText("制图");
        rightTitle.setTextColor(getResources().getColor(R.color.bule_light));
        back.setTextColor(getResources().getColor(R.color.bule_light));


//1，3折线描述
        mFiltername = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.DATA_FILTERNAME);
        mData_filternameright = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.DATA_FILTERNAMERIGHT);

        mMachinename = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.DATA_MACHINENAME);
        mMachinenameright = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.MACHINENAMERIGHT);
        mGoodsname = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.GOODSNAME);
        mGoodsnameright = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.GOODSNAMERIGHT);
//2，4折线描述
//        mFilternames = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.DATA_FILTERNAMES);
//        mData_filternamerights = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.DATA_FILTERNAMERIGHTS);
        mMachinenames = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.DATA_MACHINENAMES);
        mMachinenamerights = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.MACHINENAMERIGHTS);
        mGoodsnames = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.GOODSNAMES);
        mGoodsnamerights = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.GOODSNAMERIGHTS);

        mRbleftright = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.RBLEFTRIGHT);

        if (mRbleftright.equals("right")) {
            rbLeft.setChecked(false);
            rbRight.setChecked(true);
        } else {
            rbLeft.setChecked(true);
            rbRight.setChecked(false);
        }

        // TODO: 2018/8/2   有问题待修改

        if (!TextUtils.isEmpty(mFiltername)) {
//      左      点此定制横向坐标系
            tvFilterCondition.setText(mFiltername);
        }
//      右      点此定制横向坐标系
        if (!TextUtils.isEmpty(mData_filternameright)) {
            tvFilterConditionRight.setText(mData_filternameright);
        }

        if (!TextUtils.isEmpty(mMachinename)) {
//            增加折线描述
            tvCommunityGoods.setText(mMachinename);
        }
        if (!TextUtils.isEmpty(mGoodsname) && !TextUtils.isEmpty(mMachinename)) {
            tvCommunityGoods.setText(mMachinename + mGoodsname);
        }

        if (!TextUtils.isEmpty(mMachinenameright)) {
            tvCommunityGoodsRight.setText(mMachinenameright);
        }
        if (!TextUtils.isEmpty(mGoodsnameright) && !TextUtils.isEmpty(mMachinenameright)) {
            tvCommunityGoodsRight.setText(mMachinenameright + mGoodsnameright);
        }


        if (!TextUtils.isEmpty(mMachinenames)) {
            tvDescription.setText(mMachinenames);
        }
        if (!TextUtils.isEmpty(mGoodsnames) && !TextUtils.isEmpty(mMachinenames)) {
            tvDescription.setText(mMachinenames + mGoodsnames);
        }

        if (!TextUtils.isEmpty(mMachinenamerights)) {
            tvDescription2.setText(mMachinenamerights);
        }
        if (!TextUtils.isEmpty(mGoodsnamerights) && !TextUtils.isEmpty(mMachinenamerights)) {
            tvDescription2.setText(mMachinenamerights + mGoodsnamerights);
        }


        back.setOnClickListener(this::onClick);
        rightTitle.setOnClickListener(this::onClick);
        rbLeft.setOnClickListener(this::onClick);
        rbRight.setOnClickListener(this::onClick);
        tvFilterCondition.setOnClickListener(this::onClick);
        tvFilterConditionRight.setOnClickListener(this::onClick);
        llLeft.setOnClickListener(this::onClick);
        llRight.setOnClickListener(this::onClick);
        tvDescription.setOnClickListener(this::onClick);
        tvDescription2.setOnClickListener(this::onClick);
        tvCommunityGoods.setOnClickListener(this::onClick);
        tvCommunityGoodsRight.setOnClickListener(this::onClick);
        tvMore.setOnClickListener(this::onClick);
        tvHistoryRecord.setOnClickListener(this::onClick);

        SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.SDATE, sdate);


        rgDate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (R.id.rb_day == checkedId) {
                    sdate = "1";
                    SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.SDATE, sdate);
                } else if (R.id.rb_mouth == checkedId) {
                    sdate = "2";
                    SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.SDATE, sdate);
                }
            }
        });


        if (!"增加折线描述".equals(tvCommunityGoods.getText().toString())) {
            tvDescription.setVisibility(View.VISIBLE);
        }
        if (!"增加折线描述".equals(tvCommunityGoodsRight.getText().toString())) {
            tvDescription2.setVisibility(View.VISIBLE);
        }

//        查询的历史记录
        getHisData();

    }

    private void getHisData() {
        List<LineHistoryModel> lineHistoryModels = SQLite.select().distinct().from(LineHistoryModel.class).queryList();
//        倒叙排行
        Collections.reverse(lineHistoryModels);
        //左轴
//     右轴
        mLineHistoryAdapter = new LineHistoryAdapter(this, lineHistoryModels, new RVItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //左轴
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.DATA_STYPEID, lineHistoryModels.get(position).getStypeId());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINE_NUMBERS, lineHistoryModels.get(position).getMachineNumbers());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDS, lineHistoryModels.get(position).getGuigeids());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLV, lineHistoryModels.get(position).getIszhengzhanglv());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINE_NUMBERSS, lineHistoryModels.get(position).getMachineNumbers2());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDSS, lineHistoryModels.get(position).getGuigeids2());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLVS, lineHistoryModels.get(position).getIszhengzhanglv2());

                //     右轴
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.DATA_STYPEIDRIGHT, lineHistoryModels.get(position).getStypeId3());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINENUMBERSRIGHT, lineHistoryModels.get(position).getMachineNumbers3());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDSRIGHT, lineHistoryModels.get(position).getGuigeids3());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLVRIGHT, lineHistoryModels.get(position).getIszhengzhanglv3());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINENUMBERSRIGHTS, lineHistoryModels.get(position).getMachineNumbers4());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDSRIGHTS, lineHistoryModels.get(position).getGuigeids4());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLVRIGHTS, lineHistoryModels.get(position).getIszhengzhanglv4());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.CTYPECYLEFT, lineHistoryModels.get(position).getMctypecyleft());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.CTYPECYRIGHT, lineHistoryModels.get(position).getMctypecyright());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.SDATE, lineHistoryModels.get(position).getMsdate());

                Intent intent = new Intent(AnalysisFilterXYActivity.this, MpLineChartActivity.class);
                startActivity(intent);
            }
        });
        lvHistoryRecord.setAdapter(mLineHistoryAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getHisData();
    }

    public class LineHistoryAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater inflater;
        private List<LineHistoryModel> list;
        private RVItemClickListener rvItemClickListener;

        public LineHistoryAdapter(Context context, List<LineHistoryModel> list, RVItemClickListener rvItemClickListener) {
            super();
            this.context = context;
            inflater = LayoutInflater.from(context);
            this.list = list;
            this.rvItemClickListener = rvItemClickListener;
        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (list != null && list.size() > 0) {
                if (list.size() > 3) {
                    return 3;
                } else {
                    return list.size();
                }
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_historydata, null);
            }
            TextView tv_leftaxis = convertView.findViewById(R.id.tv_leftaxis);
            TextView tv_rightaxis = convertView.findViewById(R.id.tv_rightaxis);
            LinearLayout liearitemll = convertView.findViewById(R.id.liearitemll);

            LineHistoryModel lineHistoryModel = list.get(position);

            tv_leftaxis.setText("左轴:" + lineHistoryModel.getFiltername() + " - " + lineHistoryModel.getMachinename() + lineHistoryModel.getMachinename2());
            tv_rightaxis.setText("右轴:" + lineHistoryModel.getFiltername2() + " - " + lineHistoryModel.getMachinename3() + lineHistoryModel.getMachinename4());

            liearitemll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rvItemClickListener.onItemClick(position);
                }
            });
            return convertView;
        }
    }
}
